package com.leecode.xiehf.page_01;

/**
 * 整数数组 nums 按升序排列，数组中的值 互不相同 。
 * <p>
 * 在传递给函数之前，nums 在预先未知的某个下标 k（0 <= k < nums.length）上进行了 旋转，使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]（下标 从 0 开始 计数）。例如， [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。
 * <p>
 * 给你 旋转后 的数组 nums 和一个整数 target ，如果 nums 中存在这个目标值 target ，则返回它的索引，否则返回 -1 。
 * <p>
 *  
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/search-in-rotated-sorted-array
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class Solution_0033 {

    public static void main(String[] args) {
        Solution_0033 solution = new Solution_0033();
        int result = solution.search(
                new int[]{57, 58, 59, 62, 63, 66, 68, 72, 73, 74, 75, 76, 77, 78, 80, 81, 86, 95, 96, 97, 98, 100, 101, 102, 103, 110, 119, 120, 121, 123, 125, 126, 127, 132, 136, 144, 145, 148, 149, 151, 152, 160, 161, 163, 166, 168, 169, 170, 173, 174, 175, 178, 182, 188, 189, 192, 193, 196, 198, 199, 200, 201, 202, 212, 218, 219, 220, 224, 225, 229, 231, 232, 234, 237, 238, 242, 248, 249, 250, 252, 253, 254, 255, 257, 260, 266, 268, 270, 273, 276, 280, 281, 283, 288, 290, 291, 292, 294, 295, 298, 299, 4, 10, 13, 15, 16, 17, 18, 20, 22, 25, 26, 27, 30, 31, 34, 38, 39, 40, 47, 53, 54}
                , 30);
        System.out.println(result);
    }

    public int search(int[] nums, int target) {
        if (nums.length == 0) {
            return -1;
        }
        if (nums.length == 1) {
            if (target == nums[0]) {
                return 0;
            }
            return -1;
        }
        int len = nums.length;
        int mid = len / 2;
        return Math.max(getIndex(nums, 0, mid, target), getIndex(nums, mid, len - 1, target));
    }

    private int getIndex(int[] nums, int left, int right, int target) {
        if (left > right) {
            return -1;
        }
        if (left == right) {
            if (nums[left] == target) {
                return left;
            }
            return -1;
        }
        if (nums[left] < nums[right]) {
            // 有序
            if (nums[left] <= target && nums[right] >= target) {
                // 在有序里面
                return getIdx(nums, left, right, target);
            }
            return -1;
        }
        // 无序
        int mid = (left + right) / 2;
        return Math.max(getIndex(nums, left, mid, target), getIndex(nums, mid + 1, right, target));
    }

    private int getIdx(int[] nums, int left, int right, int target) {
        if (left > right) {
            return -1;
        }
        if (left == right) {
            if (nums[left] == target) {
                return left;
            }
            return -1;
        }
        int mid = (left + right) / 2;
        if (nums[mid] == target) {
            return mid;
        }
        if (nums[mid] < target) {
            return getIdx(nums, mid + 1, right, target);
        }
        return getIdx(nums, left, mid - 1, target);
    }

    public int search1(int[] nums, int target) {
        if (nums.length == 0) {
            return -1;
        }
        if (nums.length == 1) {
            if (target == nums[0]) {
                return 0;
            }
            return -1;
        }
        int len = nums.length;
        int mid = len / 2;
        return Math.max(getRotate(nums, 0, mid, target), getRotate(nums, mid + 1, len - 1, target));
    }

    private int getRotate(int[] nums, int left, int right, int target) {
        if (left > right) {
            return -1;
        }
        if (left == right) {
            if (nums[left] == target) {
                return left;
            } else {
                return -1;
            }
        }
        int mid = (left + right) / 2;
        int rotate = getRotate(nums, mid + 1, right, target);
        if (rotate >= 0) {
            return rotate;
        }
        int rotate2 = getRotate(nums, left, mid, target);
        if (rotate2 >= 0) {
            return rotate2;
        }
        return -1;
    }
}

